package com.example.qbb01;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

    private EditText edtPhone, edtPassword;
    private CheckBox cbAgree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        edtPhone = findViewById(R.id.edtPhone);
        edtPassword = findViewById(R.id.edtPassword);
        cbAgree = findViewById(R.id.cbAgree);
        findViewById(R.id.btnRegister).setOnClickListener(view -> clickRegisterButton());

    }

    /**
     * 点击注册按钮
     */
    private void clickRegisterButton() {
        if (edtPhone.getText().toString().trim().isEmpty()) {
            Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
        } else if (edtPassword.getText().toString().trim().isEmpty()) {
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
        } else if (cbAgree.isChecked() == false) {
            Toast.makeText(RegisterActivity.this, "请阅读并同意《服务协议》和《亲宝宝隐私政策》", Toast.LENGTH_SHORT).show();
        } else {
            String phone = edtPhone.getText().toString();
            String password = edtPassword.getText().toString();
            doRegisterHttp(phone, password);
        }
    }

    /**
     * 执行注册的网络请求
     */
    private void doRegisterHttp(String phone, String password) {
        String urlStr = "https://www.wanandroid.com/user/register";
        String paramsStr = "username=" + phone + "&password=" + password + "&repassword=" + password;
        new Thread(() -> {
            HttpUtils.doPost(urlStr, paramsStr, new HttpCallback() {
                @Override
                public void onHttpSucceed() {
                    // 此处自定义网络请求成功之后的操作
                    Intent intent = new Intent();
                    intent.putExtra("phone", phone);
                    intent.putExtra("password", password);
                    RegisterActivity.this.setResult(RESULT_OK, intent);
                    RegisterActivity.this.finish();
                }
            });
        }).start();
    }
}